package com.ajaxw.analyser;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class InvertedLists implements Serializable {
	private static final long serialVersionUID = -8149722381478911673L;
	Map<String, ArrayList<Integer>> lists = null;

	public InvertedLists() {
		this.lists = new HashMap<String, ArrayList<Integer>>();
	}

	public int getSize() {
		return this.lists.size();
	}

	public void add(String key, int id) {
		boolean exist = this.lists.containsKey(key);
		ArrayList<Integer> lst = null;
		if (exist)
			lst = this.lists.get(key);
		else {
			lst = new ArrayList<Integer>();
			this.lists.put(key, lst);
		}
		// lst.add(id);
	}

	public void add(String key, ArrayList<Integer> ids) {
		boolean exist = this.lists.containsKey(key);
		ArrayList<Integer> lst = null;
		if (exist)
			lst = this.lists.get(key);
		else {
			lst = new ArrayList<Integer>();
			this.lists.put(key, lst);
		}

		int cnt = ids.size();
		for (int i = 0; i < cnt; i++) {
			lst.add(ids.get(i));
		}
	}

	public void dump2screen() {
		Iterator<String> it = this.lists.keySet().iterator();
		while (it.hasNext()) {
			String key = it.next();
			ArrayList<Integer> docs = this.lists.get(key);
			System.out.println("*** " + key);
			for (int doc : docs) {
				System.out.print(String.format("%-8d", doc));
			}
			System.out.println();
		}
	}
}
